/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBClass;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.PegawaiBean;

/**
 *
 * @author elfatah
 */
public class AccountServlet extends HttpServlet {

    String pass, passLama, pass2, pass1, id, usernames;
    PegawaiBean bean = new PegawaiBean();
    DBClass dbobject = new DBClass();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
//    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        try {
//            /* TODO output your page here. You may use following sample code. */
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet AccountServlet</title>");
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet AccountServlet at " + request.getContextPath() + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
//        } finally {
//            out.close();
//        }
//    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //  processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            //processRequest(request, response);
            usernames = request.getParameter("usernames");
            passLama = request.getParameter("passwordLama");
            pass1 = request.getParameter("passwordBaru");
            pass2 = request.getParameter("KpasswordBaru");
            id = request.getParameter("id");
            HttpSession session = request.getSession();
            //   List list = dbobject.cekLogin(usernames, passLama);
            if (session.getAttribute("pass").equals(passLama)) {
                if (pass1.equals(pass2)) {
                    dbobject.UpdateAccount(usernames, pass1, id);
                    session.setAttribute("username", usernames);
                    session.setAttribute("pass", pass1);
                    out.print("<script>window.alert('Perubahan data akun sukses');window.location=('password')</script>");
                } else {
                    out.print("<script>window.alert('Konfirmasi password baru gagal. Password tidak sama');window.location=('password')</script>");

                }
            } else {
                out.print("<script>window.alert('Password lama anda salah');window.location=('password')</script>");

            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
